/******************************************************************************
* This file is part of is32p218 platform,
* Copyright (c) 2009 HangZhou InfoStrong CO.,LTD. All rights reserved.
* This software may only be used under the terms of a valid, current,
* end user license from InfoStrong.
* Nothing else gives you the right to use this software.   
*
* Name:   sys_glb.h
* Desc:   System global header file
* Author: Jerry
* Date:   2009-10-27
* Note:   
* History:
*
******************************************************************************/
#include "def.h"
#include "os_api.h"

#ifndef __SYS_GLB_H__
#define __SYS_GLB_H__
#ifdef __cplusplus
extern "C" {
#endif

/* Chip and SW information definitions */

// Version format: S(xx).(xx).(xx)(R/D)<_xxx> ; slave version
// D(xx).(xx).(xx)(R/D)<_xxx> ; dp_node version

#define INFO_SDK_VER				"XN5106C_AMR_SDK_V2.09.00"

/* Task priority definitions */
#define TSK_PRIO_HOST_IF			0x01
#define TSK_PRIO_PLC_IF				0x02
#define TSK_PRIO_PLC_HL				0x03
#define TSK_PRIO_APP 				0x04
#define TSK_PRIO_INT_CHECK			0x05
#define TSK_PRIO_REG_CHECK			OS_LO_PRIO

#define TSK_STK_SIZE_HOST_IF		64
#ifdef APP_SDK_FLG
#define TSK_STK_SIZE_APP			192
#else
#define TSK_STK_SIZE_APP			128
#endif
#define TSK_STK_SIZE_PLC_HL			128
#define TSK_STK_SIZE_PLC_IF			88

/* FLAG Bit definitions */
// HOST Communication Flags definitions
#define FLG_BIT_HOST_IF_DATA		0x00000001
#define FLG_BIT_HOST_IF_ERR			0x00000002
#define FLG_BIT_HOST_SELF			0x00000004

// PLC Flags definitions
#define FLG_BIT_PLC_IF_DATA			0x00000100
#define FLG_BIT_PLC_IF_ERR			0x00000200
#define FLG_BIT_PLC_IF_BLD_FULL		0x00000400
#define FLG_BIT_PLC_IF_SND_OK		0x00000800
#define FLG_BIT_PLC_CSMA_OK			0x00001000
#define FLG_BIT_PLC_CSMA_ERR		0x00002000

// Idle tsk Check PLC_if tsk Flag
#define FLG_BIT_PLC_IF_TSK_ALIVE		0x00004000

// Memory partitions definitions
#define SYS_MEM_CHECK_FLG	1		// Memory check flag

#define MEM_SYS_BLK_SIZE	OS_MEM_SIZE(256)
#ifdef APP_SDK_FLG
#define MEM_SYS_BLK_NUM		16
#else
#define MEM_SYS_BLK_NUM		11
#endif
#define MEM_SYS_BLK_NUM_MIN	3

#define MEM_SYS_BLK16_SIZE	OS_MEM_SIZE(32)
#define MEM_SYS_BLK16_NUM	6

// Queue length definitions
#define APP_MSG_ITEM_LEN	24
#define PLC_MSG_ITEM_LEN	16
typedef struct
{
	isOS_U16 m_msg_id;
	isOS_U16 m_len;
	isOS_U32 m_pointer;
}MSG_HDR_T;

typedef struct
{
	isOS_U8 m_msg_id;
	isOS_U8 m_rec_byte;
}MSG_HDR_HOST_T;


#define GLB_INFO_MAGIC_ID	0x40472549	// "@G%I"
#define GLB_INFO_OFFSET		0x0400		// Offset from region start address
typedef struct
{
	U32 m_magic_id;
	S8 *m_p_chipid;
	S8 *m_p_version; 
	S8 *m_p_bld_time;
	S8 *m_p_bld_ver; 
	U32 **m_p_crc32; 
	S8 *m_p_hw_ver;
}GLB_INFO_MAP_T;

// Application Flags
extern isOS_FLG g_sys_flg;
extern const GLB_INFO_MAP_T c_glb_info;

void sys_glb_init( void );
void *sys_malloc( U16 size );
BOOL sys_free( void *p_mem );
U8 sys_mem_free_num( void );

#ifdef __cplusplus
}
#endif
#endif // end __SYS_GLB_H__
